/*
 *  Copyright (C) 2011 Hatsites <Hatsites_AT_gmail.com>
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License
 *  as published by the Free Software Foundation; either version 2
 *  of the License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

package org.myorg.shr.dtb.adaptator;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
import java.sql.DriverManager;
import java.sql.ResultSet;

/**
 *
 * @author Hatsites
 *
 * email: Hatsites_AT_gmail.com
 * ... please remove "_AT_" from the above string to get the right email address
 *
 */

public class AdaptatorJdbc extends AdaptatorAbs{
    private  AdaptatorJdbc() throws Exception
    {
        if(!_bln_debug_){
        Class.forName("com.mysql.jdbc.Driver");
        this._strUser_ = "none";
        }
    }

    public static synchronized AdaptatorJdbc s_getInstance()
            throws Exception {
        if (_INSTANCE_ == null) {
                _INSTANCE_ = new AdaptatorJdbc();
        }
        return _INSTANCE_;
    }

    @Override
    public Object clone() throws CloneNotSupportedException {
        throw new CloneNotSupportedException();
    }

    public String getUser(){
        return this._strUser_;
    }

    public void connect(
            String strUrl,
            String strUser,
            String strPassword) throws Exception
    {
        
        if(!_bln_debug_){
        this._strUser_ = strUser;
        this._con_ = (Connection) DriverManager.getConnection(
                strUrl,
                strUser,
                strPassword);
        this._stmt_ =    (Statement) this._con_.createStatement();
        }
        return;
    }
    public void disconnect() throws Exception{
        if(!_bln_debug_){
        this._con_.close();
        this._strUser_ = "none";
        }
        return;
    }


    public void executeUpdate(String strCommand) throws Exception
    {
        if (!_bln_debug_){
            this._stmt_.executeUpdate(strCommand);
        }
        if (!_bln_mute_){
            System.out.println(strCommand);
        }
        return;
    }

    public void executeQuerry(String strCommand) throws Exception {
        if (!_bln_debug_){
            System.out.println(strCommand);
            this._res_ = this._stmt_.executeQuery(strCommand);
        }
        else{
            System.out.println(strCommand);
        }
        return;
    }
   public ResultSet getResult(){
       return _res_;
   }

    private static AdaptatorJdbc _INSTANCE_;
    private Statement _stmt_;
    private Connection _con_;
    private String _strUser_;
    private ResultSet _res_;
    private Boolean _bln_debug_ = false;
    private Boolean _bln_mute_ = false;
}
